import requests
from bs4 import BeautifulSoup
from docx import Document
from docx.shared import Inches
import os
class BaiduBug:
    """百度贴吧爬虫类。该类有以下参数：文件头，requests请求，文件解析，帖子名称，总发帖数，总页数,帖子列表"""
    def __init__(self, url):
        super(BaiduBug, self).__init__()
        self.url = url
        self.getInfo(self.url)
        self.postList=[]
    def getInfo(self,url):
        self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
        self.wb_data = requests.get(url,headers=self.headers)
        self.soup = BeautifulSoup(self.wb_data.text,'lxml')
        self.title=self.soup.select("h3")[0].text;
        self.postNumber=self.soup.select(".l_reply_num > .red")[0].text
        self.pageNumber=self.soup.select(".l_reply_num > .red")[1].text
        print("帖子名称:{0},回帖数:{1},共{2}页".format(self.title,self.postNumber,self.pageNumber));
    def getPage(self,pageNum):
        pageData = requests.get(self.url+"?pn="+str(pageNum),headers=self.headers)
        pageSoup=BeautifulSoup(pageData.text,'lxml')
        postlistSelector= pageSoup.select(".l_post");
        for postItem in postlistSelector:
            post=Post();
            author=str(postItem.select(".d_name")[0].text).replace("\n","")
            headimgurlArray=postItem.select("img[username]")
            headimgurl="http://tb2.bdstatic.com/tb/static-pb/img/head_80.jpg"
            if(len(headimgurlArray)>0):
                if(headimgurlArray[0]['src']!="//tb2.bdstatic.com/tb/static-pb/img/head_80.jpg"):
                    headimgurl=headimgurlArray[0]['src']
                else:
                    headimgurl=headimgurlArray[0]['data-tb-lazyload']

            contentHtml=postItem.select(".j_d_post_content")
            content=""
            badgeTitle=""
            badgelv=""
            time=""
            if(len(contentHtml)>0):
                for contentVar in contentHtml[0]:
                    if(contentVar.string!=None):
                        content+=str(contentVar)+"\n"
            badgeTitleArray=postItem.select(".d_badge_title")
            if(len(badgeTitleArray)>0):
                badgeTitle=badgeTitleArray[0].text
            badgelvArray=postItem.select(".d_badge_lv")
            if(len(badgelvArray)>0):
                badgelv=badgelvArray[0].text
            infoArray=postItem.select(".tail-info")
            if(len(infoArray)>0):
                time=infoArray[len(infoArray)-1].text
            #print(badgeTitle)
            #print(badgelv)
            #print(time)
            print(content)
            post.setAuthor(author)
            post.setAuthorImg(headimgurl,author+".jpg")
            post.setContent(content)
            post.setBadgeTitle(badgeTitle)
            post.setBadgelv(badgelv)
            post.setTime(time)
            self.postList.append(post)

class Post:
    """获取所有的帖子，该类有以下参数：作者，头像，内容，称号，等级，发帖时间，楼层数"""
    def __init__(self):
        super(Post, self).__init__()
    def setAuthor(self,author):#作者
        self.author=author
    def setAuthorImg(self,headimgurl,imgName):#作者头像
        #首先从url中下载头像，存到本地headImage文件夹底下，然后在把头像位置设置为本地
        r=requests.get(headimgurl)
        with open('./headImg/'+imgName,'wb')as f:
            for chunk in r.iter_content(chunk_size=32):
                f.write(chunk)
        self.headImg='./headImg/'+imgName
    def setContent(self,content):
        self.content=content
    def setBadgeTitle(self,badgeTitle):
        self.badgeTitle=badgeTitle
    def setBadgelv(self,bagelv):
        self.bagelv=bagelv
    def setTime(self,time):
        self.time=time

class Lzl:
    """获取楼中楼"""
    def __init__(self):
        super(Lzl, self).__init__()
    def setName(self,lzlName):
        self.lzlName=lzlName
    def setContent(self,content):
        self.content=content
    def setTime(self,time):
        self.time=time

if __name__ == '__main__':
    if not os.path.exists("./headImg/"):
      os.makedirs("./headImg/")
    tieurl=input("请输入帖子的地址:")
    littleBug=BaiduBug(tieurl)
    for page in range(int(littleBug.pageNumber)):
        littleBug.getPage(page+1)
        document = Document()
        document.add_heading(littleBug.title, 0)
        for post in littleBug.postList:
            document.add_picture(post.headImg,width=Inches(1.25))
            document.add_paragraph(post.author)
            document.add_paragraph(post.badgeTitle+":"+post.bagelv)
            document.add_paragraph(post.content)
            document.add_paragraph(post.time)
            document.add_paragraph("-----------------\n\n");
    document.save(littleBug.title+'.docx')
